package cn.waka.sys.admin.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.waka.sys.admin.dao.IMenuDao;
import cn.waka.sys.admin.dao.INodeDao;
import cn.waka.sys.admin.object.Menu;
import cn.waka.sys.admin.object.Node;

@Service
public class MenuService {
	
	private IMenuDao menuDao;
	private INodeDao nodeDao;

	public List<Menu> findTopAll(String authorize) {
		if (authorize == null) {
			return null;
		}
		
		String[] auths = authorize.split(",");
		for (int i = 0; i < auths.length; i++) {
			boolean status = menuDao.findAuthStatus(auths[i]);
			if (!status) {
				auths[i] = null;
			}
		}
		List<Menu> menus=menuDao.findMenuTree(auths);
		Map<Integer, Menu> mapAll =new TreeMap<Integer,Menu>();
		for(Menu m:menus){
			mapAll.put(m.getId(), m);
		}
		List<Menu> rs = new LinkedList<Menu>();
		
		for (Menu menu : menus) {
			if (menu.getPid() == 0) {
				rs.add(menu);
			} else {
				int pid = menu.getPid();
				if (mapAll.containsKey(pid)) {
					mapAll.get(pid).add(menu);
				}
			}
		}
		return rs;
	}

	public List<Menu> selectedMenus() {
		List<Menu> menus = menuDao.selectedMenus();
		for (Menu menu : menus) {
			Node node = nodeDao.getNodeById(menu.getNodeId());
			if (node == null) {
				node = new Node();
				node.setId(0);
				node.setNode("-");
			}
			menu.setNode(node);
		}
		return menus;
	}

	public List<Menu> findMenus() {
		List<Menu> menus=menuDao.findMenus();
		Map<Integer, Menu> msAll =new HashMap<Integer,Menu>();
		for(Menu m:menus){
			msAll.put(m.getId(), m);
		}
		
		for (Menu m : menus) { 
			String key="";
			if(m.getPid()==0){
				key = ""+m.getId();
			}else {
				key = m.getId()+"-"+msAll.get(m.getPid()).getKey();
			}
			msAll.get(m.getId()).setKey(key);
		}
		List<Menu> ms=new ArrayList<Menu>();
		for (Menu m : msAll.values()) { 
			if(m.getPid()==0){
				ms.add(m);
			}else{
				msAll.get(m.getPid()).add(m);
			}
		} 

		ms = arrTree(ms);
		return ms;
	}

	private List<Menu> arrTree(List<Menu> list) {
		List<Menu> arrlist = new ArrayList<Menu>();
		for(Menu m : list){
			int k = m.getKey().split("-").length;
			String spl=StringUtils.repeat("&nbsp;&nbsp;&nbsp;├&nbsp;&nbsp;",k-1);
			m.setKey(spl);
			arrlist.add(m);
			if(m.getChildren()!=null){
				List<Menu> _arrlist = arrTree(m.getChildren());
				arrlist.addAll(_arrlist);
			}
		}
		
		return arrlist;
	}
	public Menu findMenuById(int id) {
		Menu menu = menuDao.findMenuById(id);
		Node node = nodeDao.getNodeById(menu.getNodeId());
		if (node == null) {
			node = new Node();
			node.setId(0);
			node.setNode("-");
		}
		menu.setNode(node);
		return menu;
	}

	public int save(Menu menu) {
		if (menu == null) {
			return -1;
		}
		int rs = 0;
		if (menu.getId() == 0) {
			rs = menuDao.insert(menu);
		} else {
			rs = menuDao.update(menu);
		}

		return rs;
	}

	public int updateStatus(int status, int id) {
		return menuDao.updateStatus(status, id);
	}

	public int updateSort(int sort, int id) {
		return menuDao.updateSort(sort, id);
	}

	public int delete(int id) {
		int rs = menuDao.findChildren(id);
		if (rs > 0) {
			return -1;
		}
		int a = menuDao.del(id);
		return a;
	}

	@Autowired
	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	@Autowired
	public void setNodeDao(INodeDao nodeDao) {
		this.nodeDao = nodeDao;
	}

}
